package awesome.Manager;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class PictureManager extends Manager {

	public PictureManager() {
		super("picture");
	}

	
	public void createItemImage(String prefix, String filename, String caption, int uid, int itemId) {

		System.out.println(caption);
		
		Picture picture = new Picture(1, caption, 400, 300, 200, 150, prefix
				+ "images_" + filename, prefix + "thumbs_" + filename, prefix + filename,
				"images/images_" + filename, "images/thumbs_" + filename);

		
		this.resize(picture);
		this.insert(picture, uid, itemId);
		
	}  
	
	
	public void create(String prefix, String filename, String caption, int uid) {
		
		Picture picture = new Picture(1, caption, 400, 300, 200, 150, prefix
				+ "images_" + filename, prefix + "thumbs_" + filename, prefix + filename,
				"images/images_" + filename, "images/thumbs_" + filename);

		this.resize(picture);
		this.insert(picture, uid);
	}

	private void insert(Picture p, int uid, int itemId) {
		this.db.openConnection();
		Connection conn = this.db.getConnection();
		Statement s;
		try {
			s = conn.createStatement();
			
			s.execute("insert into picture (pid, caption, imageWidth," +
					"imageHeight, imageFileName, thumbWidth , thumbHeight ," +
					"thumbFileName, uid, iid) values(" +
					"'', '"+p.getCaption()+"','"+ p.getImageWidth() +"','" +
					p.getImageHeight() +"','" + p.getDBImage() +"','" +
					p.getThumbWidth() +"','" + p.getThumbHeight() +"','" +
					p.getDBThumb()+"','"+uid +"','" + itemId + "');");
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	} 

	private void insert(Picture p, int uid) {
		this.db.openConnection();
		Connection conn = this.db.getConnection();
		Statement s;
		try {
			s = conn.createStatement();
			
			s.execute("insert into picture (pid, caption, imageWidth," +
					"imageHeight, imageFileName, thumbWidth , thumbHeight ," +
					"thumbFileName, uid) values(" +
					"'', '"+p.getCaption()+"','"+ p.getImageWidth() +"','" +
					p.getImageHeight() +"','" + p.getDBImage() +"','" +
					p.getThumbWidth() +"','" + p.getThumbHeight() +"','" +
					p.getDBThumb()+"','"+ uid +"');");
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
		public void resize(Picture p) {
			
			createImage(p.getImageWidth(), p.getImageHeight(),
					p.getRegularFileName(), p.getImageFileName());
			
			
			createImage(p.getThumbWidth(), p.getThumbHeight(),
					p.getRegularFileName(), p.getThumbFileName());
            
	}
		
		
		public String createImage(int width, int height, String regularfilename, String filename)
		{
			  ImageIcon myImageIcon = new ImageIcon(regularfilename);
	           
	           java.awt.Image image = myImageIcon.getImage();
	             
				 int thumbWidth=width;
	             int thumbHeight=height;
	             BufferedImage thumbImage = new BufferedImage(thumbWidth, 
	thumbHeight, BufferedImage.TYPE_INT_RGB);
	             java.awt.Graphics2D graphics2D = 
	thumbImage.createGraphics();
	             graphics2D.setRenderingHint(java.awt.RenderingHints.KEY_INTERPOLATION,java.awt.RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	             graphics2D.drawImage(image, 0, 0, thumbWidth, 
	thumbHeight, null);
	             
	              System.out.println(filename);
	            try {
					
	            	 FileOutputStream out = new FileOutputStream(new File(filename));
	 					
					 JPEGImageEncoder encoder = 
						 JPEGCodec.createJPEGEncoder(out);
						              JPEGEncodeParam param = 
						 encoder.getDefaultJPEGEncodeParam(thumbImage);
						              int quality = 100;
						              param.setQuality((float)quality / 100.0f, false);
						              encoder.setJPEGEncodeParam(param);
						              encoder.encode(thumbImage);
						              out.close();
						              	              
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			return null;
		}
		
		/*
	private void insertBlob(Picture p) {
	
		
		try {
		
		String sql = "insert into picture (pid, caption, imageWidth," +
		"imageHeight, imageFileName, thumbWidth , thumbHeight ," +
		"thumbFileName, regularBlob) values(" +
		"'', '"+p.getCaption()+"','"+ p.getImageWidth() +"','" +
		p.getImageHeight() +"','" + p.getImageFileName() +"','" +
		p.getThumbWidth() +"','" + p.getThumbHeight() +"','" +
		p.getThumbFileName()+"', ?);";
		
	//	String sql = " INSERT INTO picture (caption, picture) VALUES ('"
//				+ picture.getCaption() + "',?) ";
	
		this.db.openConnection();
		
		PreparedStatement pStmt = this.db.getConnection().prepareStatement(sql);
		
		
		//	pStmt.setBinaryStream(1, new ByteArrayInputStream(p.getRegularBlob().toByteArray()), p.getRegularBlob().size());
		//	pStmt.setBinaryStream(2, new ByteArrayInputStream(), 0);
		//	pStmt.setBinaryStream(3, new ByteArrayInputStream(), 0);

			pStmt.execute();
		//	this.db.getConnection().commit(); 
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}*/
	
		public String getPicLoc(int iid)
		{	
			String picName = "images/go.jpg";
			String query = "SELECT * FROM picture WHERE iid = " + iid;
			ResultSet rs = db.getResultSet(query);
			if(rs != null){
				try {
					if(rs.next()){
						 picName=rs.getString("imageFileName");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
			return picName;
		}
	
		public String getThumbLoc(int iid)
		{
			String picName = "images/go.jpg";
			String query = "SELECT * FROM picture WHERE iid = " + iid;
			ResultSet rs = db.getResultSet(query);
			if(rs != null){
				try {
					if(rs.next()){
						 picName=rs.getString("thumbFileName");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			return picName;
		}
}
